package com.org.wareon.main;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class ImageManipulation {

}

class Screenshot extends JPanel {

	private BufferedImage image;

	public Screenshot(double scaleX, double scaleY) throws AWTException {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();
		System.out.println(dim.getWidth());
		BufferedImage screenshot = (new Robot())
				.createScreenCapture(new Rectangle(0, 0, (int) dim.getWidth(),
						(int) dim.getHeight()));
		Graphics g = screenshot.getGraphics();
		int w = screenshot.getWidth();
		int h = screenshot.getHeight();
		BufferedImage after = new BufferedImage(w, h,
				BufferedImage.TYPE_INT_ARGB);
		AffineTransform at = new AffineTransform();
		at.scale(scaleX, scaleY);
		AffineTransformOp scaleOp = new AffineTransformOp(at,
				AffineTransformOp.TYPE_BILINEAR);
		after = scaleOp.filter(screenshot, after);

		this.image = (after);
		this.paint(g);
		g.dispose();
		
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(image, 0, 0, null); // see javadoc for more info on the
										// parameters
	}

}

// //////////////////////////////////////////////////////////PaintWindow
class PaintWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	JLayeredPane lpane = new JLayeredPane();
	ModelImage imageModel;
	PaintPanel canvas;
	
	// ====================================================== constructor
	public PaintWindow() throws AWTException {
		// --- create the buttons
		this.imageModel=new ModelImage(createSreenshot());
		this.canvas = new PaintPanel(0.5,imageModel);
		JButton circleButton = new JButton("Circle");
		circleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				canvas.setShape(PaintPanel.CIRCLE);
			}
		});
		JButton rectangleButton = new JButton("Rectangle");
		rectangleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				canvas.setShape(PaintPanel.RECTANGLE);
			}
		});
		
		// --- layout the buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(2, 1));
		buttonPanel.add(circleButton);
		buttonPanel.add(rectangleButton);

		// --- layout the window
		Container content = this.getContentPane();
		content.setLayout(new BorderLayout());
		content.add(buttonPanel, BorderLayout.WEST);
		content.add(canvas, BorderLayout.CENTER);
		this.setSize(1366/2, 768/2);
		this.setTitle("Paint Demo");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		//this.pack();
	}// end constructor

	public BufferedImage createSreenshot() throws AWTException {

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();
		System.out.println(dim.getWidth());
		BufferedImage screenshot = (new Robot())
				.createScreenCapture(new Rectangle(0, 0, (int) dim.getWidth(),
						(int) dim.getHeight()));
		return screenshot;
	}

}// endclass PaintWindow

// /////////////////////////////////////////////////////////// PaintPanel
class PaintPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener {
	// --- Public constants used to specify shape being drawn.
	public static final int NONE = 0;
	public static final int LINE = 1;
	public static final int RECTANGLE = 2;
	public static final int CIRCLE = 3;

	// --- Variables to store the current figure info
	private int _shape = NONE;
	private int _currentStartX = 0; // where mouse first pressed
	private int _currentStartY = 0;
	private int _currentEndX = 0; // where dragged to or released
	private int _currentEndY = 0;
	private int _currentClickX = 0; // where dragged to or released
	private int _currentClickY = 0;
	// --- BufferedImage to store the underlying saved painting.
	// Will be initialized first time paintComponent is called.
	private BufferedImage _bufImage = null;

	// --- Private constant for size of paint area.
	private static final int SIZE = 600; // size of paint area
	private Color cLR = Color.red;
	private ModelImage imageModel;
	private double scale;

	// ====================================================== constructor
	public PaintPanel(double scale,ModelImage imageModel) throws AWTException {
		this.imageModel=imageModel;
		this.scale=scale;
		Graphics g = imageModel.getImage().getGraphics();
		int w = imageModel.getImage().getWidth();
		int h = imageModel.getImage().getHeight();
		BufferedImage after = new BufferedImage(w, h,
				BufferedImage.TYPE_INT_ARGB);
		AffineTransform at = new AffineTransform();
		at.scale(scale, scale);
		AffineTransformOp scaleOp = new AffineTransformOp(at,
				AffineTransformOp.TYPE_BILINEAR);
		after = scaleOp.filter(imageModel.getImage(), after);
		this._bufImage = (after);
		this.paint(g);
		g.dispose();
		// --- Add the mouse listeners.
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addKeyListener(this);
	}// endconstructor

	// ========================================================= setShape
	public void setShape(int shape) {
		// --- Provided so users can set the shape.
		_shape = shape;
	}// end setShape

	// =================================================== paintComponent
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g; // downcast to Graphics2D
		cLR = new Color(100,10,10,122);
		g2.setColor(cLR);
		g2.drawImage(_bufImage, null, 0, 0); // draw previous shapes
		drawCurrentShape(g2);
	}// end paintComponent

	// ================================================= drawCurrentShape
	private void drawCurrentShape(Graphics2D g2) {
		// --- Draws current shape on a graphics context, either
		// on the context passed to paintComponent, or the
		// context for the BufferedImage.
		int x,y,xNew = 0,yNew = 0;
		double dagree;
		double laenge;
		switch (_shape) {
		case NONE:
			break;

		case CIRCLE:
			g2.setColor(cLR);
			x= _currentEndX-_currentClickX;
			y= _currentEndY- _currentClickY;
			laenge=Math.sqrt(x*x+y*y);
			if((x==0) || (y==0)){
				if(x==0)
					dagree=90;
				else
					dagree=0;
			}
			else{
				dagree=Math.toDegrees(Math.atan((double)((double)y/(double)x)));
				dagree=Math.sqrt(dagree*dagree);

			}
			
			xNew=(int) (Math.cos(Math.PI*dagree/180)*laenge);
			yNew=(int) (Math.sin(Math.PI*dagree/180)*laenge);

			_currentStartX=_currentClickX+xNew;
			_currentStartY=_currentClickY+yNew;
			xNew=_currentClickX-xNew;
			yNew=_currentClickY-yNew;
			//System.out.println((int)Math.sqrt((_currentStartX-xNew)*(_currentStartX-xNew)));
			g2.fillOval(xNew, yNew, (int)Math.sqrt((_currentStartX-xNew)*(_currentStartX-xNew)), (int)Math.sqrt((_currentStartY-yNew)*(_currentStartY-yNew)));
			
			this.repaint();
			break;

		case RECTANGLE:
			g2.setColor(cLR);
			x= _currentEndX-_currentClickX;
			y= _currentEndY- _currentClickY;
			if(x<0 && y<0){
				g2.fillRect(_currentClickX+x, _currentClickY+y, x*-1,y*-1);
			}else
			{
				if(x<0){
					g2.fillRect(_currentClickX+x, _currentClickY, x*-1,y);
				}else{
					if(y<0){
						g2.fillRect(_currentClickX, _currentClickY+y, x,y*-1);
					}else
					{
						g2.fillRect(_currentStartX, _currentStartY, _currentEndX
								- _currentStartX, _currentEndY - _currentStartY);
					}
				}
			}
			
			this.repaint();
			break;

		default: // should never happen
			g2.drawString("Huh?", 10, 20);
			break;
		}
	}// end paintComponent

	// ===================================================== mousePressed
	public void mousePressed(MouseEvent e) {
		_currentClickX = e.getX(); // save x coordinate of the click
		_currentClickY = e.getY(); // save y
		_currentStartX = _currentClickX; // save x coordinate of the click
		_currentStartY = _currentClickY; // save y
		_currentEndX = _currentClickX; // set end to same pixel
		_currentEndY = _currentClickY;
		this.repaint();

	}// end mousePressed

	// ===================================================== mouseDragged
	public void mouseDragged(MouseEvent e) {
		_currentEndX = e.getX(); // save new x and y coordinates
		_currentEndY = e.getY();
		// show new shape
	}// end mouseDragged

	// ==================================================== mouseReleased
	public void mouseReleased(MouseEvent e) {
		_currentEndX = e.getX(); // save ending coordinates
		_currentEndY = e.getY();
		
		// --- Draw the current shape onto the buffered image.
		Graphics2D grafarea = _bufImage.createGraphics();
		
		//drawCurrentShape(grafarea);
		this.repaint();
	}// end mouseReleased

	// ========================================== ignored mouse listeners
	public void mouseMoved(MouseEvent e) {
	}
	public void keyPressed(KeyEvent e) {
		System.out.println(e.getKeyChar());
		if(e.getKeyChar() == e.VK_ENTER) {
			int x= _currentEndX-_currentClickX;
			int y= _currentEndY- _currentClickY;
			// put the code you want to execute when Enter is pressed here
			if(x<0 && y<0){
				imageModel.setRouletteTable(_currentClickX+x, _currentClickY+y, x*-1,y*-1,scale);
			}else
			{
				if(x<0){
					imageModel.setRouletteTable(_currentClickX+x, _currentClickY, x*-1,y,scale);
				}else{
					if(y<0){
						imageModel.setRouletteTable(_currentClickX, _currentClickY+y, x,y*-1,scale);
					}else
					{
						imageModel.setRouletteTable(_currentStartX, _currentStartY, _currentEndX
								- _currentStartX, _currentEndY - _currentStartY,scale);
					}
				}
			}

			_bufImage=imageModel.getRouletteTable();
			this.repaint();
		}
	}

	// unused abstract methods
	public void keyTyped(KeyEvent e) {
		System.out.println(e.getKeyChar());
	}

	public void keyReleased(KeyEvent e) {
		System.out.println(e.getKeyChar());
	} 
	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
	}


}// endclass PaintPanel